package com.ygxsk.carhome.views.picker;

import android.content.Context;
import android.util.AttributeSet;

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * 日期选择
 * Created by ycuwq on 17-12-28.
 */
public class DayPicker extends WheelPicker<Integer> {

    private int mMinDay, mMaxDay;

    private int mSelectedDay;


    //private int mYear, mMonth;

    private long mMaxDate, mMinDate;

    private boolean mIsSetMaxDate;

    /**
     * 因为它处于当前滚轮的最后一个级别，所以在这里有一个年月日的生成格式最终的时间
     */
    //private String selectedFormatTime = "";

    private OnDaySelectedListener mOnDaySelectedListener;

    public DayPicker(Context context) {
        this(context, null);
    }

    public DayPicker(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public DayPicker(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        setItemMaximumWidthText("000");
        NumberFormat numberFormat = NumberFormat.getNumberInstance();
        numberFormat.setMinimumIntegerDigits(2);
        setDataFormat(numberFormat);

        mMinDay = 1;
        mMaxDay = Calendar.getInstance().getActualMaximum(Calendar.DATE);
        updateDay();
        mSelectedDay = Calendar.getInstance().get(Calendar.DATE);
        setSelectedDay(mSelectedDay, false);
        setOnWheelChangeListener(new OnWheelChangeListener<Integer>() {
            @Override
            public void onWheelSelected(Integer item, int position, String itemString) {

                mSelectedDay = item;

                //LogUtils.d("itchen---DayPicker---滚轮选中--->" + item);

                if (mOnDaySelectedListener != null) {
                    mOnDaySelectedListener.onDaySelected(item, itemString);
                }

            }
        });
    }


    public void setMonth(int year, int month) {

        Calendar calendar = Calendar.getInstance();
        calendar.clear();//尽量清除一次，兼容android4.2

        calendar.setTimeInMillis(mMaxDate);

        int maxYear = calendar.get(Calendar.YEAR);
        int maxMonth = calendar.get(Calendar.MONTH) + 1;
        int maxDay = calendar.get(Calendar.DAY_OF_MONTH);

        //如果不判断mIsSetMaxDate，则long 为0，则选择1970-01-01 时会有问题
        if (mIsSetMaxDate && maxYear == year && maxMonth == month) {
            mMaxDay = maxDay;
        } else {
            calendar.set(year, month - 1, 1);
            mMaxDay = calendar.getActualMaximum(Calendar.DATE);
        }

        //Log.d(TAG, "setMonth: year:" + year + " month: " + month + " day:" + mMaxDay);

        //LogUtils.d("itchen---DayPicker---选中设置的 年，月，日 是---->" + "setMonth: year:" + year + "--month:" + month + "--最大day:" + mMaxDay);

        calendar.setTimeInMillis(mMinDate);

        int minYear = calendar.get(Calendar.YEAR);
        int minMonth = calendar.get(Calendar.MONTH) + 1;
        int minDay = calendar.get(Calendar.DAY_OF_MONTH);
        if (minYear == year && minMonth == month) {
            mMinDay = minDay;
        } else {
            mMinDay = 1;
        }

        updateDay();

        if (mSelectedDay < mMinDay) {
            setSelectedDay(mMinDay, false);
        } else if (mSelectedDay > mMaxDay) {
            setSelectedDay(mMaxDay, false);
        } else {
            setSelectedDay(mSelectedDay, false);
        }

        //那么最终的 年月日 就被确定了，就可以算出 formatTime 的准确时间
        //setSelectedFormatTime(mYear,);
        //LogUtils.d("itchen----->DayPicker--选中的  selectedFormatTime --->"+selectedFormatTime);

    }

//    public void setSelectedFormatTime(int selectedYear,int selectedMonth,int selectedDay){
//        selectedFormatTime = selectedYear+"."+selectedMonth+"."+selectedDay;
//    }

//    public String getSelectedFormatTime(){
//        return selectedFormatTime;
//    }

    public int getSelectedDay() {
        return mSelectedDay;
    }

    public void setSelectedDay(int selectedDay) {
        setSelectedDay(selectedDay, true);
    }

    public void setSelectedDay(int selectedDay, boolean smoothScroll) {
        setCurrentPosition(selectedDay - mMinDay, smoothScroll);
        mSelectedDay = selectedDay;
    }

    public void setMaxDate(long date) {
        mMaxDate = date;
        mIsSetMaxDate = true;
    }

    public void setMinDate(long date) {
        mMinDate = date;
    }

    public void setOnDaySelectedListener(OnDaySelectedListener onDaySelectedListener) {
        mOnDaySelectedListener = onDaySelectedListener;
    }

    private void updateDay() {

        //原始代码
        List<Integer> list = new ArrayList<>();

        List<String> listString = new ArrayList<>();

        for (int i = mMinDay; i <= mMaxDay; i++) {
            list.add(i);
            listString.add(String.format("%02d", i) + "日");
        }

        setDataList(list, listString);
    }

    public interface OnDaySelectedListener {
        void onDaySelected(int day, String dayString);
    }
}
